/**
 * Created by hubery on 2017/8/8.
 */
import axios from 'axios';
import {
  getindexnewsdata,
  getmenuData,
  getbanner,
  getindexnumber
} from "./sort";
import store from '../store/index'
import {
  getpricecat,
  getpriceknamecat
} from "./price";

/***
 *  获取首页新闻，首页菜单
 */
export function getindexAlldata() {

  let obj = {};
  let bannerparams = {
    place: "index"
  }
  let bannerhome = {
    channel: "home"
  }
  let bannerptcl = {
    channel: "tcl"
  }
  let bannerpdc = {
    channel: "dc"
  }
  let bannerpdlg = {
    channel: "dlg"
  }
  axios.all([getindexnewsdata(), getmenuData(), getbanner(bannerparams), getindexnumber(bannerptcl), getindexnumber(bannerpdc), getindexnumber(bannerpdlg), getindexnumber(bannerhome)])
    .then(axios.spread(function (news, menu, banner, datatcl, datadc, datadlg, dataBanner) {
      // console.log('两个请求都完成了', news)
      // console.log('两个请求都完成了menu', menu)

      if (news.code === '0000') {
        obj.newsList = news.data;
      }
      if (menu.code === '0000') {
        let temp = menu.data;
        //全局處理后台返回linkcatid，变成cid，！！！很重要
        temp.forEach(ele => {
          ele.cid = ele.linkcatid;
          let tempChild = ele.child;
          tempChild.forEach(eleChild => {
            eleChild.cid = eleChild.linkcatid;
          });
        });
        var menuList = temp;
        obj.menuList = temp;
        let tempMenu = {};
        let tempMenuAll = {};
        for (let index = 0; index < menuList.length; index++) {
          const element = menuList[index];
          tempMenuAll[element.channel] = element.child
          if (element.child.length > 5) {
            tempMenu[element.channel] = element.child.slice(0, 4)
            tempMenu[element.channel].push({
              channel: element.channel,
              linkcatid: "",
              menuid: "",
              menuname: "全品种",
              orderid: "",
              pmenuid: "",
            })
          } else {
            tempMenu[element.channel] = element.child
          }
        }
        obj.menuListpart = tempMenu;
        obj.menuAllListpart = tempMenuAll;

      }

      if (banner.code === '0000') {
        obj.banner = banner.data;
      }
      if (dataBanner.code === '0000') {
        obj.dataBanner = dataBanner.data;
      }
      if (datatcl.code === '0000') {
        obj.datatcl = datatcl.data;
      }
      if (datadc.code === '0000') {
        obj.datadc = datadc.data;
      }
      if (datadlg.code === '0000') {
        obj.datadlg = datadlg.data;
      }
      console.log(datatcl, datadc, datadlg)
      store.dispatch('acIndexData', obj)
    }));
  return obj;
}

export function getPriceMenu(params) {
  let obj = {};
 
  axios.all([getpricecat(params), getpriceknamecat(params)])
    .then(axios.spread(function (pricelis, menu) {
// console.log(pricelis)
// console.log(menu)
    }));
  return obj;
}
